package com.filepd.controller;

import com.filepd.common.R;
import com.filepd.service.FileService;
import com.filepd.utils.WeBASEUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/ftp")
@Slf4j
public class FTPController {


    @Autowired
    private WeBASEUtils weBASEUtils;


    @Autowired
    private FileService fileService;

    /**
     * 上传文件
     *
     * @param file
     * @return
     */
    @PostMapping(consumes = "multipart/*", headers = "content-type=multipart/form-data")
    public R<String> uploadFile(@RequestParam(required = false) String userName, @RequestParam("file") MultipartFile file) {
        log.info("开始上传文件...");

        R<String> result = fileService.uploadFile(userName, file);

        return result;
    }

    /**
     * 文件下载
     *
     * @param fileName
     * @param ftpFilePath
     * @param response
     */

    // TODO 做个数据库,存放文件名和文件下载地址 直链下载
    @GetMapping("/download")
    public R<String> downloadFile(@RequestParam String fileName, @RequestParam String ftpFilePath, HttpServletResponse response) {
        log.info("文件开始下载...");

        R<String> result = fileService.downloadFileToFtp(fileName, ftpFilePath, response);

        return result;
    }

    /**
     * 获取所有文件
     *
     * @return
     */
    @GetMapping("/all")
    public R<List<String>> filesFromFtpDirectory() {
        log.info("开始获取文件...");

        R<List<String>> result = fileService.getFilesFromFtpDirectory();

        return result;
    }

}